package com.example.auth.filter;

import com.example.auth.utils.ResponseUtil;
import com.example.auth.utils.Result;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author: LiYang
 * @create: 2023-07-28 09:55
 * @Description:
 * @Version 1.0
 **/
@Component
public class AuthEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        if(authException instanceof BadCredentialsException){
            ResponseUtil.out(response, Result.fail(authException.getMessage()));
        }
        ResponseUtil.out(response, Result.fail(authException.getMessage()));
    }
}
